package com.cyan.app.workflow.activity.TQM;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.cyan.app.workflow.AppData;
import com.cyan.app.workflow.R;
import com.cyan.app.workflow.base.activity.BaseFragmentActivity;
import com.cyan.app.workflow.bean.BmsfshBean;
import com.cyan.app.workflow.bean.CommonBean;
import com.cyan.app.workflow.common.HttpConfig;
import com.cyan.app.workflow.customview.ShowTipDialog;
import com.cyan.app.workflow.util.AppUtils;
import com.cyan.app.workflow.util.network.GetRequestParams;
import com.google.gson.Gson;

import org.json.JSONObject;

/**
 * Created by DXW on 2017-04-26.
 */

public class BmsIPQCDetailActivity extends BaseFragmentActivity {

    private BmsfshBean.Bmsfsh rerultTqmpoolBean;
    private EditText editTextAccqty;
    private EditText editTextType;
    private EditText editTextqcqty;
    private EditText editTextRemarks;
    private EditText  editTextNgqty;

    private  double ngQty;
    private  double qcQty;
    private  String remark;
    private  String badcode;

    private ShowTipDialog showdialog = new ShowTipDialog();
    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ipqc_bmsbarcode_detail);
        titleBar();
        initView();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 0x0002 && resultCode == RESULT_OK){
            String result = "";
            if(data != null) {
                result = data.getStringExtra("result");
                editTextType.setText(String.valueOf(result));
            }
        }

    }

    private void titleBar() {
        TextView mTextView =(TextView)findViewById(R.id.top_title_info);
        mTextView.setText("质检确认");
        Button back = (Button) findViewById(R.id.top_title_back);
        back.getBackground().setAlpha(255);// 透明度0~255透明度值 ，值越小越透明
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //返回按钮
                finish();
            }
        });
    }
    private void initView() {

        rerultTqmpoolBean = (BmsfshBean.Bmsfsh) getIntent().getSerializableExtra("ipqcBarcodeDetail");
        View resultView =  findViewById(R.id.tqm_manno_detail_result_lay);

        TextView mEditTextQc = (TextView) findViewById(R.id.tqm_manno_detail_result_qc);
        mEditTextQc.setText("抽检 " + rerultTqmpoolBean.getBarcodeQty());

        TextView mEditTextOk = (TextView) findViewById(R.id.tqm_manno_detail_result_ok);
        mEditTextOk.setText("良品 " +  (rerultTqmpoolBean.getBarcodeQty()));

        TextView mEditTextNg = (TextView) findViewById(R.id.tqm_manno_detail_result_ng);
        mEditTextNg.setText("不良 0"  );


        editTextType = (EditText) findViewById(R.id.tqm_manno_detail_type);
        editTextAccqty = (EditText)findViewById(R.id.tqm_manno_detail_accqty);
        editTextAccqty.setText(rerultTqmpoolBean.getBarcodeQty().toString());

        editTextqcqty = (EditText)findViewById(R.id.tqm_manno_detail_qcqty);
        editTextNgqty = (EditText)findViewById(R.id.tqm_manno_detail_ngqty);
        editTextNgqty.setText("0");

        editTextRemarks = (EditText)findViewById(R.id.tqm_manno_detail_remarks);


        Button typeButton=(Button)findViewById(R.id.tqm_manno_detail_type_sm);
        typeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(BmsIPQCDetailActivity.this, TQMBadReasonActivity.class);
                intent.putExtra("TQMKind","1");
                intent.putExtra("ProcedureNo",rerultTqmpoolBean.getProcedureNo());
                startActivityForResult(intent, 0x0002);
            }
        });


        Button submitButton=(Button)findViewById(R.id.tqm_manno_detail_submit);
        submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String[] obj = buildResultObj();
                if(obj[0]=="0"){
                    AppUtils.showTip(BmsIPQCDetailActivity.this,obj[1]);
                }else {
                    submitAsyncLoader task =  new submitAsyncLoader();
                    task.execute("IPQCConfirm");
                }
            }
        });
    }
    private String[] buildResultObj(){
        String[] result = new String[]{"0",""};

        if(editTextqcqty.getText().length()==0||Double.parseDouble(editTextqcqty.getText().toString())<0){
            result[1]="抽检数量必须大于0";
            return result;
        }


        if(editTextNgqty.getText().length()==0||Double.parseDouble(editTextNgqty.getText().toString())<0){
            result[1]="不良数量必须大于等于0";
            return result;
        }


        qcQty = Double.parseDouble(editTextqcqty.getText().toString());
        if(editTextNgqty.getText().length()==0)
            ngQty = 0;
        else
            ngQty = Double.parseDouble(editTextNgqty.getText().toString());
        if(ngQty>0){
            if(editTextType.getText().length()==0) {
                result[1] = "请选择不良或报废类型";
                return result;
            }
        }
        badcode = editTextType.getText().toString();

        if(rerultTqmpoolBean.getBarcodeQty()< qcQty){
            result[1] = "抽检数量不能大于送检数量。";
            return result;
        }
        if(ngQty > qcQty){
            result[1] = "不良数量不能大于抽检数量。";
            return result;
        }
        remark = editTextRemarks.getText().toString();

        result[0] = "1";
        return result;
    }
    public class submitAsyncLoader extends AsyncTask<String, Integer, String> {
        @Override
        protected void onPreExecute() {
            showdialog.showDialog(BmsIPQCDetailActivity.this);
        }

        @Override
        protected String doInBackground(String... params) {
            String result = null;
            String action = "IPQCConfirm";
            try{
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("Barcode", rerultTqmpoolBean.getBarcode());
                jsonObject.put("OID", rerultTqmpoolBean.getOID());
                jsonObject.put("QcQty", qcQty);
                jsonObject.put("BadQty", ngQty);
                jsonObject.put("BadCode", badcode);
                jsonObject.put("Remarks", remark);
                jsonObject.put("FactoryID", HttpConfig.Main_FactoryID);
                jsonObject.put("UserNo", AppData.getInstance().getUserId());
                jsonObject.put("Connectstring", AppData.getInstance().getConnectstring());
                result= GetRequestParams.getRequestResult(HttpConfig.HTTP_MAIN_SERVER_URL, action, jsonObject.toString(), getApplicationContext());
            }catch (Exception e){
                Log.e("error", e.toString());
            }
            return result;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (s == null||s.isEmpty()||s.contains("html")) {
                AppUtils.showTip(BmsIPQCDetailActivity.this,"操作异常，请稍后再试！");
                showdialog.closeDialog();
                return;
            }
            CommonBean commonBean = new Gson().fromJson(s, CommonBean.class);
            if (commonBean.getCode()== 0) {
                Intent intent = new Intent();
                intent.putExtra("OID",rerultTqmpoolBean.getOID());
                BmsIPQCDetailActivity.this.setResult(RESULT_OK,intent);
                AppUtils.showTip(BmsIPQCDetailActivity.this,"品质确认成功。");
                showdialog.closeDialog();
                finish();
            }else {
                BmsIPQCDetailActivity.this.setResult(RESULT_CANCELED);
                AppUtils.showTip(BmsIPQCDetailActivity.this,"品质确认失败，"+commonBean.getMsg());
                showdialog.closeDialog();
            }
        }

        @Override
        protected void onCancelled(String s) {
            BmsIPQCDetailActivity.this.setResult(RESULT_CANCELED);
        }
    }
}
